package com.explorer.ider.bootguide.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.explorer.ider.bootguide.MainActivity;
import com.explorer.ider.bootguide.R;
import com.explorer.ider.bootguide.utils.QRCodeUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;

import static com.google.android.gms.internal.zzip.runOnUiThread;

/**
 * Created by Eric on 2017/9/19.
 */

public class FragmentTwoDimen extends Fragment {


    private View views;
    private ImageView imageView;
    private Button skip;
    private Method systemProperties_get = null;
    private String sn;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (views == null){
            views = inflater.inflate(R.layout.fragment_two_dimen, container,false);
            imageView = (ImageView) views.findViewById(R.id.image_view);
            skip = (Button)views.findViewById(R.id.complete);
            skip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toNextPager();
                }
            });
            final Animation mAnimationIn , mAnimationOut;
            mAnimationIn = AnimationUtils.loadAnimation(getActivity(), R.anim.view_focus_animation_in);
            mAnimationOut = AnimationUtils.loadAnimation(getActivity(),R.anim.view_focus_animation_out);
            mAnimationIn.setFillAfter(true);
            mAnimationOut.setFillAfter(true);
            skip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.startAnimation(mAnimationIn);
                    }else {
                        v.startAnimation(mAnimationOut);
                    }
                }
            });
            initView();
        }
        return views;
    }

    private void initView(){
        final String filePath = getFileRoot(getContext()) + File.separator
                + "qr_" + System.currentTimeMillis() + ".jpg";
        sn = getAndroidOsSystemProperties("ro.boot.serialno");
        if (sn.equals("")){
            sn = getAndroidOsSystemProperties("ro.serialno");
        }
        if (!sn.equals(""))
        Log.i("FragmentTwoDimen", sn);
        final String info = sn+" \n"+getEthMac();
        //二维码图片较大时，生成图片、保存文件的时间可能较长，因此放在新线程中
        if (!info.equals("")) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    boolean success = QRCodeUtil.createQRImage(info, 300, 300,
                            //BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
                            null,
                            filePath);

                    if (success) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
                            }
                        });
                    }else {
                        toNextPager();
                    }
                }
            }).start();
        }else {
            toNextPager();
        }
    }
    private String getFileRoot(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File external = context.getExternalFilesDir(null);
            if (external != null) {
                return external.getAbsolutePath();
            }
        }

        return context.getFilesDir().getAbsolutePath();
    }
    private String getAndroidOsSystemProperties(String key) {
        String ret;
        try {
            systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
            if ((ret = (String) systemProperties_get.invoke(null, key)) != null)
                return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return "";
    }
    private void toNextPager(){
        Intent intent = new Intent();
        intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER5);
        getActivity().sendBroadcast(intent);

    }

    public String getEthMac() {
        File file = new File("/sys/class/net/eth0/address");
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String mac = br.readLine();
            if (!mac.equals(""))
            Log.i("FragmentTwoDimen", mac);
            return mac;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

}
